package com.example.team.dao.Imp;

import com.example.team.dao.ILeavesDao;
import com.example.team.entity.Leaves;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/****
 * 编写者：王轩
 * 时间：2023/10/30
 * 请假管理
 */
@Repository
public class ILeavesDaoImpl implements ILeavesDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //添加方法
    @Override
    public int insertLeaves(Leaves leaves) {
        return jdbcTemplate.update("insert into leaves(`LeaveID`,`EmployeeID`,`StartDate`,`EndDate`,`Reason`,`Ratifiers`)"+
                "values(?,?,?,?,?,?)",
                leaves.getLeaveID(),
                leaves.getEmployeeID(),
                leaves.getStartDate(),
                leaves.getEndDate(),
                leaves.getReason(),
                leaves.getRatifiers());
    }

    @Override
    public int deleteLeaves(Leaves leaves) {
        return jdbcTemplate.update("delete from leaves where LeaveID = ?", leaves.getLeaveID());
    }

    @Override
    public int updateLeaves(Leaves leaves) {
        return jdbcTemplate.update("update leaves set EmployeeID = ?,StartDate = ?,EndDate = ?,Reason = ?,Ratifiers = ? ",
                leaves.getEmployeeID(),
                leaves.getStartDate(),
                leaves.getEndDate(),
                leaves.getReason(),
                leaves.getRatifiers());
    }

    @Override
    public List<Leaves> getAllLeaves(Leaves leaves) {
        return jdbcTemplate.query("select `LeaveID`,`EmployeeID`,`StartDate`,`EndDate`,`Reason`,`Ratifiers` "+"from leaves",
                new Object[]{},
                new BeanPropertyRowMapper<>(Leaves.class));
    }

    @Override
    public Leaves getLeaves(Leaves leaves) {
        List<Leaves>result =jdbcTemplate.query("select"+"*"+"from leaves where LeaveID = '"+leaves.getLeaveID()+"'",
                new Object[]{},
                new BeanPropertyRowMapper<>(Leaves.class));
        if(result.size()==0)
            return null;
        else
            return result.get(0);
    }

    @Override
    public int insterApprover(Leaves leaves) {
        return jdbcTemplate.update(
                "update leaves set Ratifiers = ? where LeaveID = ?",
                leaves.getRatifiers(),
                leaves.getLeaveID()
        );
    }
}
